$(function() {
	
    var mdEditor = editormd("md-body", {
        width: "90%",
        height: 740,
        path: '../../static/app/editormd/lib/',
        theme: "default",
        previewTheme: "default",
        editorTheme: "default",
        markdown: $("#md-temp").attr("md"),
        codeFold: true,
        saveHTMLToTextarea: true, 
        searchReplace: true,
        htmlDecode: "style,script,iframe|on*",  
        emoji: true,
        taskList: true,
        tocm: true, 
        tex: true, 
        flowChart: true, 
        sequenceDiagram: true, 
        imageUpload: true,
        imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
        //imageUploadURL : "./php/upload.php",
        onload: function() {
            console.log('onload', this);
        }
    });

    $("#save").click(function() {
        var name = $("#docName").val();
        if (null == name || name.length == 0){
        	alert("文件名称不能为空。");
        	return;
        }
        var id = $("#md-id").attr("docid");
        var tag = $("#docTag").val();
        var description = $("#description").val();
        var visibleRange = $("input:checked[name=visibleRange]").val();
        var docInfo = {
    		"id": id,
            "name": name,
            "tag": tag,
            "description": description,
            "visibleRange": visibleRange,
            "mdDetail": mdEditor.getMarkdown(),
            "htmlDetail": mdEditor.getHTML()
        };
        $.post("/doc/update", docInfo, function(data) {
        	if(data.retCode == 0){
        		alert("更新成功");
        	} else{
        		alert("更新失败");
        	}
        });
    });

    $("#preview").click(function() {
        alert(mdEditor.getHTML());
    });

    $("#show-btn").bind('click', function() {
        mdEditor.show();
    });

    $("#hide-btn").bind('click', function() {
        mdEditor.hide();
    });

    $("#preview-btn").bind('click', function() {
        mdEditor.previewing();
    });

});
